import time

from appium.webdriver import Remote
from appium.webdriver.common.appiumby import AppiumBy
from selenium.common import TimeoutException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.find_img import FindImg
from common.report_add_img import add_img_2_report
from common.tools import get_project_path, build_path


class Base:

    def find_element_new(self, driver: Remote, locator_type=AppiumBy.XPATH, locator_value=None, timeout=10):
        try:
            element = WebDriverWait(driver, timeout).until(
                EC.presence_of_element_located((locator_type, locator_value))
            )
            return element
        except(TimeoutException, NoSuchElementException):
            print(f"元素未找到：{locator_type},{locator_value}")
            return None

    def find_elements_new(self, driver: Remote, locator_type=AppiumBy.XPATH, locator_value=None, timeout=10):
        try:
            element = WebDriverWait(driver, timeout).until(
                EC.presence_of_all_elements_located((locator_type, locator_value))
            )
            return element
        except(TimeoutException, NoSuchElementException):
            print(f"元素未找到：{locator_type},{locator_value}")
            return None

    def send_keys_new(self, driver: Remote, locator_type=AppiumBy.XPATH, locator_value=None, timeout=10, value=''):
        try:
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element.clear()
            element.send_keys(value)
        except Exception as e:
            print(f"在元素{locator_type, locator_value}中输入数据失败{str(e)}")

    def click_new(self, driver: Remote, locator_type=AppiumBy.XPATH, locator_value=None, timeout=10):
        try:
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element.click()
        except Exception as e:
            print(f"点击元素{locator_type, locator_value}失败：{str(e)}")

    def get_text(self, driver: Remote, locator_type=AppiumBy.XPATH, locator_value=None):
        element = self.find_element_new(driver, locator_type, locator_value)
        try:
            element_text = element.text
            print(f"获取到的文本：{element_text}")
            return element_text
        except Exception as e:
            print(f"元素text获取失败，错误信息为{str(e)}")

    def find_img_in_source(self, driver, img_name):
        # 截图后保存图片的路径
        source_img_path = get_project_path() + build_path("img", "source_img", img_name, add_sep_before=True)

        # 需要查找的图片路径
        search_img_path = get_project_path() + build_path("img", "assert_img", img_name, add_sep_before=True)

        driver.get_screenshot_as_file(source_img_path)

        time.sleep(3)

        confidence = FindImg().get_confidence(source_img_path, search_img_path)

        return confidence


class BaseAssert:
    def assert_equal_screen_shot(self, driver, exceptor, step_name):
        if not isinstance(exceptor, tuple):
            print("exceptor参数类型错误，必须传元组类型")
        else:
            print(f"正在断言{exceptor[0]}是否等于{exceptor[1]}")
            try:
                assert exceptor[0] == exceptor[1]
                add_img_2_report(driver, step_name=step_name)
            except Exception as e:
                print(f"断言失败：{str(e)}")
                add_img_2_report(driver, step_name=step_name)
                raise e
